#include <TFile.h>
#include <iostream>
#include <string>
#include <TTree.h>
#include <TH1D.h>
#include <TF1.h>
#include <TCanvas.h>
using namespace std;
string filePath = "../AnaData/";
void averageOf8ps()
{
    string fileName="ToF_Ana_20251028_1";
    TCanvas *c1 = new TCanvas();
    // TFile *file = new TFile("../RootData/ToF_Sort_Coin_ToFdata_0916.root", "read");
    TFile *file = new TFile((filePath + fileName + ".root").c_str(), "read");
    TTree *treeAna_ToF = new TTree();
    TH1F*averageHist=new TH1F("averageHist","averageHist",2000,-0.4,-0.3);
    // file->GetObject("treeCoin_ToF", treeAna_ToF);
    file->GetObject("treeAna_ToF", treeAna_ToF);
    double tmDif[4][4];
    treeAna_ToF->SetBranchAddress("tmDif_Tstart",tmDif);
    for (int iEvent = 0;iEvent<treeAna_ToF->GetEntries()-100;iEvent++){
        double sum = 0;
        for (int i = 0; i < 100; i++)
        {
            treeAna_ToF->GetEntry(iEvent + i);
            sum+=tmDif[0][2];
            
        }
        averageHist->Fill(sum/100.0);
    }
        

    // TF1* f1 = new TF1("f1", "gaus", -2, 0);
    averageHist->Draw();
    c1->SetTitle(fileName.c_str());
    c1->Draw();
    c1->WaitPrimitive();
}